//
//  TSHomeMsgTableView.m
//  chart
//

//
//  TSTaskTableView.m
//  chart
//
//  Created by 曾斌 on 16/3/24.
//  Copyright (c) 2016年 xu. All rights reserved.
//

#import "TSHomeMsgTableView.h"

#import "TSRecieveMsgTableView.h"
#import "TSMessageListCell.h"
#import "TSRecieverListModel.h"
#import "TSMsgEditTableViewController.h"
#import "TSHomeViewController.h"
#import "TSTaskListModel.h"
#import "TSHomeMsgCell.h"
#import "TSTaskIngDetailController.h"
#import "TSAuditTaskDetailController.h"
#import "TSTools.h"
#import "TSReceiveTabBarController.h"
#import "TSAuditTabBarController.h"
#import "TSSuperviseTabBarController.h"
#import "TSFinishTabBarController.h"
#import "TSMsgTabBarController.h"
#import "TSTools.h"
@implementation TSHomeMsgTableView{
    TSTaskListModel * taskListModel;
    NSArray * taskList;
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self=[super initWithFrame:frame style:style]) {
        
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        
        self.dataSource = self;
        self.delegate = self;
    }
    taskListModel =[[TSTaskListModel alloc] init];
    
    return self;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //定义个静态字符串为了防止与其他类的tableivew重复
    static NSString *CellIdentifier =@"Cell";
    //定义cell的复用性当处理大量数据时减少内存开销
    TSHomeMsgCell *cell = [self dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell ==nil)
    {
        cell = [[TSHomeMsgCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell initUI];
    }
    
    if (taskList) {
        TSTaskModel *model = [taskList objectAtIndex:indexPath.row];
        if(model){
            if([model.type isEqualToString:@"待审核任务"]){
                [cell setTitle:@"待审核任务"];
              cell.imageView.image = [UIImage imageNamed:@"home_msg_1"];
            }else if([model.type isEqualToString:@"待审核的消息"]){
              //cell.textLabel.text  =  @"待审核任务";
                [cell setTitle:@"待审核的消息"];
                cell.imageView.image = [UIImage imageNamed:@"home_msg_2"];
            }else if([model.type isEqualToString:@"待进行任务"]){
                // cell.textLabel.text  =  @"审核不通过任务";
                [cell setTitle:@"待进行任务"];
                cell.imageView.image = [UIImage imageNamed:@"home_msg_3"];
            }else if([model.type isEqualToString:@"新收到的消息"]){
                 //cell.textLabel.text  =  @"新通知消息";
                [cell setTitle:@"新收到的消息"];
                cell.imageView.image = [UIImage imageNamed:@"home_msg_4"];
            }else if([model.type isEqualToString:@"待监督任务"]){
                 //cell.textLabel.text  =  @"待监督任务";
                [cell setTitle:@"待监督任务"];
                cell.imageView.image = [UIImage imageNamed:@"home_msg_5"];
            }else if([model.type isEqualToString:@"待评审任务"]){
                 //cell.textLabel.text  =  @"待评审任务";
                [cell setTitle:@"待评审任务"];
                cell.imageView.image = [UIImage imageNamed:@"home_msg_6"];
            }
        }
        [cell setNum:model.num];
        if(![TSTools isEmpty:model.content]){
            [cell setDesc:[NSString stringWithFormat:@"任务内容:%@",model.content]];
        }else{
            [cell setDesc:@""];
        }
       
        if(model.time){
            [cell setDate: [TSTools getDateMonthDay:model.time]];
        }
      
    }
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return [taskList count] ;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (taskList) {
        TSTaskModel *model = [taskList objectAtIndex:indexPath.row];
        if([model.type isEqualToString:@"待审核的消息"]){
            [[self getViewController] presentViewController: [[TSAuditTabBarController alloc] init] animated:YES completion:nil];
        }else if([model.type isEqualToString:@"待审核任务"]){
            [[self getViewController] presentViewController: [[TSAuditTabBarController alloc] init] animated:YES completion:nil];
        }else if([model.type isEqualToString:@"待进行任务"]){
            [[self getViewController] presentViewController: [[TSReceiveTabBarController alloc] init] animated:YES completion:nil];
        }else if([model.type isEqualToString:@"新收到的消息"]){
            [[self getViewController] presentViewController: [[TSMsgTabBarController alloc] init] animated:YES completion:nil];
        }else if([model.type isEqualToString:@"待监督任务"]){
            [[self getViewController] presentViewController: [[TSSuperviseTabBarController alloc] init] animated:YES completion:nil];
        }else if([model.type isEqualToString:@"待评审任务"]){
            [[self getViewController] presentViewController: [[TSFinishTabBarController alloc] init] animated:YES completion:nil];
        }
    }
}

-(void)toNormalController:(TSTaskModel*)model{
    TSTaskIngDetailController *detail_vc = [[TSTaskIngDetailController alloc] init];
    detail_vc.taskModel = model;
    detail_vc.taskId = model.tId;
    [[self getViewController].navigationController pushViewController:detail_vc animated:YES];
    
}
-(void)toAuditDetailController:(TSTaskModel*)model{
    TSAuditTaskDetailController *detail_vc = [[TSAuditTaskDetailController alloc] init];
    detail_vc.taskModel = model;
    detail_vc.taskId = model.tId;
    [[self getViewController].navigationController pushViewController:detail_vc animated:YES];
    
}



- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


#pragma mark - requestData

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    [taskListModel fetchWithPath:self.requestUrl completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (taskListModel.list.count > 0) {
            strongSelf.footer.hidden = NO;
            strongSelf->taskList = taskListModel.list;
            [weakSelf reloadData];
            [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_Badge_Number object:[NSString stringWithFormat:@"%d",[weakSelf getMsgCount]]];
        }
    } NoProgress:NO];
}

-(int)getMsgCount{
    int totalMsg = 0;
    for(TSTaskModel *model in taskList){
        totalMsg+=model.num;
    }
    return totalMsg;
}
@end

